package com.learn.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.core.domain.LCompetitionSort;
import com.learn.core.mapper.LCompetitionSortMapper;
import com.learn.core.service.ILCompetitionSortService;
import com.learn.framework.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LCompetitionSortServiceImpl
 * @date 2021.07.07 09:53
 */
@Service
public class LCompetitionSortServiceImpl extends BaseServiceImpl<LCompetitionSortMapper, LCompetitionSort> implements ILCompetitionSortService {
    @Autowired
    LCompetitionSortMapper lCompetitionSortMapper;


    @Override
    public List<LCompetitionSort> queryLCompetitionSortAll(QueryWrapper<LCompetitionSort> queryWrapper) {
        List<LCompetitionSort> treeMenus =new ArrayList<>();
        List<LCompetitionSort> lCompetitionSorts = lCompetitionSortMapper.selectList(queryWrapper);
        // 查询树结构
        for(LCompetitionSort lCompetitionSort : getRootNode(lCompetitionSorts)) {
            lCompetitionSort=buildChilTree(lCompetitionSort,lCompetitionSorts);
            treeMenus.add(lCompetitionSort);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private LCompetitionSort buildChilTree(LCompetitionSort pNode,List<LCompetitionSort> menuList){
        List<LCompetitionSort> chilMenus =new ArrayList<>();
        for(LCompetitionSort menuNode : menuList) {
            if(menuNode.getParentNode() == pNode.getCompetitionSortId()) {
                chilMenus.add(buildChilTree(menuNode,menuList));
            }
        }
        pNode.setCompetitionSort(chilMenus);
        return pNode;
    }

    //获取根节点
    private List<LCompetitionSort> getRootNode(List<LCompetitionSort> menuList) {
        List<LCompetitionSort> rootMenuLists =new  ArrayList<LCompetitionSort>();
        for(LCompetitionSort menuNode : menuList) {
            if(menuNode.getParentNode() == 0) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }
}
